package Service;

import Dao.Impl.AccountDaoImpl;
import Dao.Impl.PetDaoImpl;
import Dao.Impl.PetOwnerImpl;
import Dao.Impl.PetStoreImpl;
import Entity.account;
import Entity.pet;
import Entity.pet_owner;
import Entity.pet_store;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AccountServiceImpl implements AccountService {
    PetDaoImpl petDao = new PetDaoImpl();
    PetOwnerImpl petOwner = new PetOwnerImpl();
    AccountDaoImpl accountDao = new AccountDaoImpl();
    PetStoreImpl petStore = new PetStoreImpl();
    DateFormat format2 = new SimpleDateFormat("yyyyMMddhhmmss");//获取系统时间
    String  s = format2.format(new Date());
    @Override
    public void insert(int name_id, int pet_id) throws SQLException, ParseException {
        pet pet=petDao.AllGetPet(pet_id);
        pet_store store = petStore.AllGetPetStore(pet.getStore_id());
        pet_owner pet_owner = petOwner.AllGetPetOwner(name_id);
        account a=new account();
        a.setSeller_id(name_id);
        a.setPet_id(pet_id);
        a.setDeal_type("1");
        a.setPrice(pet.getPrice());
        a.setBuyer_id(store.getId());
        a.setDeal_time(format2.parse(s));
        if (pet_owner.getMoney()>=pet.getPrice())
        {
            int money  = pet_owner.getMoney();
            pet_owner.setMoney(money-pet.getPrice());
            store.setBalance(store.getBalance()+pet.getPrice());
            pet.setOwner_id(pet_owner.getId());

            try{
                petStore.update(store);
                petOwner.update(pet_owner);
                petDao.update(pet);
                accountDao.add(a);
                System.out.println("购买成功");
                System.out.println("******购买账单信息********");
                System.out.println("账单id："+a.getId());
                System.out.println("宠物id："+a.getPet_id());
                System.out.println("宠物名字："+pet.getName());
                System.out.println("宠物价格："+pet.getPrice());
                System.out.println("剩余元宝数："+pet_owner.getMoney());
            }
            catch (Exception e)
            {
                System.out.println("******账单添加失败********");
            }

        }
        else {
            System.out.println("******元宝不足不能购买********");
        }


    }

    @Override
    public void insert(int name_id, int pet_id, int store_id) throws SQLException, ParseException {
        pet pet=petDao.AllGetPet(pet_id);
        pet_store store = petStore.AllGetPetStore(pet.getStore_id());
        pet_owner pet_owner = petOwner.AllGetPetOwner(name_id);
        account a=new account();
        pet_store pet_store =petStore.AllGetPetStore(store_id);
        a.setSeller_id(store_id);
        a.setPet_id(pet_id);
        a.setDeal_type("2");
        a.setPrice(pet.getPrice());
        a.setBuyer_id(name_id);
        a.setDeal_time(format2.parse(s));
        if (pet_store.getBalance()>=pet.getPrice())
        {
            int money  = pet_owner.getMoney();
            pet_owner.setMoney(money+pet.getPrice());
            int balance = pet_store.getBalance();
            pet_store.setBalance(balance-pet.getPrice());
            petStore.update(pet_store);
            petOwner.update(pet_owner);
            pet.setStore_id(store_id);
            petDao.idnull(pet);
            accountDao.add(a);
            System.out.println("卖出成功");
            System.out.println("******卖出宠物账单信息********");
            System.out.println("账单id："+a.getId());
            System.out.println("宠物id："+a.getPet_id());
            System.out.println("宠物名字："+pet.getName());
            System.out.println("宠物价格："+pet.getPrice());
            System.out.println("剩余元宝数："+pet_owner.getMoney());
        }
        else {
            System.out.println("******元宝不足不能购买********");
        }

    }

    @Override
    public List<account> GetAccountbystore(int id) throws SQLException {
        List<account> accounts = accountDao.GetAccountbystore(id);
        return accounts;
    }

}
